<?php
declare (strict_types=1);

namespace app\api\controller;



use app\model\StaffModel;
use app\model\StaffRoleModel;
use think\facade\Request;
use think\facade\Validate;

class Staff
{
    public function list()
    {
        $params = Request::param();
        $username = $params['username'] ?? '';
        $model = StaffModel::where('staff_id', '>', 0);
        if($username){
            $model->where("username","like",'%'.$username.'%');
        }
        $staffList =$model->select();
        foreach ($staffList as &$value){
            //操作员
            $staffRoleOne = StaffRoleModel::find($value['role_id']);
            $value['role_name'] = $staffRoleOne['role_name'];
        }
        unset($value);
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'staffList' => $staffList,
            ],
        ];
        return json($data);
    }
    public function add()
    {
        // 1. 获取参数
        // 2. 校验参数
        // 3. 验证文本是否合规。
        // 4. 写入数据库
        // 5. 返回数据


        //order增加员工
        $params = Request::param();
        $validate = Validate::rule([
            'username|用户名' => 'require',
            'password|密码' => 'require',
            'password2|再次输入密码' => 'require',
            'role_id|角色id' => 'require',
//            'staff_id|员工id' => 'require'
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }
        if ($params['password']
            && ($params['password'] !== $params['password2'])) {
            $data = [
                'status' => 10002,
                'message' => '两次密码输入不一致',
                'data' => []
            ];
            return json($data);
        }

//        if (!$params['staff_id']){
//            $params['staff_id']=null;
//        }
        $params['add_time']=$params['update_time']=time();
        $params['password'] = StaffModel::encrypt($params['password']);         //密码加密
        $result= StaffModel::create($params);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    // 员工删除
    public function delete()
    {
        $staffId = Request::param("staff_id");
        $validate = Validate::rule([
            'staff_id|员工id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['staff_id' => $staffId])) {
            echo $validate->getError();
            exit();
        }
//        $staff=StaffModel::find($staffId);
        $result = StaffModel::destroy($staffId);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    //员工修改
    public function update()
    {
        // 1. 获取评论id
        // 2. 修改评论状态status = 2就可以了。
        $params = Request::param();
        $validate = Validate::rule([
            'staff_id|员工id'=>'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['staff_id'=>$params['staff_id']])) {
            echo $validate->getError();
            exit();
        }

        $staff = StaffModel::find($params['staff_id']);
//
        if ($params['username']){
            $staff['username']=$params['username'];
        }
        if ($params['password'] && $params['password2']){
            if ($params['password']
                && ($params['password'] !== $params['password2'])) {
                $data = [
                    'status' => 10002,
                    'message' => '两次密码输入不一致',
                    'data' => []
                ];
                return json($data);
            }
            $staff['password'] = StaffModel::encrypt($params['password']);         //密码加密
        }
        if ($params['role_id']){
            $staff['role_id']=$params['role_id'];
        }
        $staff['update_time'] = time();
        $result = $staff->save();

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' => $result ? true : false
            ]
        ];
        return json($data);
    }
}
